<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Service\Upload as UploadService;
use App\Service\SysFile as SysFileService;

#[AutoController]
class UploadController extends AbstractController
{
    public function image()
    {
        $file = $this->request->file('iFile');
        if ($file->isValid()) {
            $upload = new UploadService();
            $data = $upload->upload($file);

            $data['file_type'] = 'image';
            $data['group_id'] = $this->request->input('group_id', 0);
            
            // 添加文件记录
            (new SysFileService())->fileSave($data);
            
            $result = ['uid' => $data['name'], 'name' => $data['real_name'], 'status' => 'done', 'url' => $data['url']];
            return $this->returnSuccess($result, '上传成功');
        } else {
            return $this->returnError('上传文件不存在');
        }
    }
}
